layui.define(['form', 'layedit', 'laydate', 'element'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let element = layui.element;
    let mapDataType = obj.channelServiceAreaVO.mapDataType;
    document.domain = "xiujiadian.com";

    let channelServiceAreaVO = obj.channelServiceAreaVO;

    let BaiduMap = {
        polygon: null,
        circle: null,
        point: null,
        map: null,
        initLng: 0,
        initLat: 0,
        createBaiduMap: function () {
            // 百度地图API功能1
            let map = new BMap.Map("allmap", {enableMapClick: false});
            map.centerAndZoom(this.point, 15);
            map.enableScrollWheelZoom();

            this.initCounty(map);

            //增加多边形
            map.addOverlay(this.polygon);
            this.map = map;
        },
        submitData: function () {
            // 父页面接收数据ID: baiduMapDataId
            //alert(BaiduMap.polygon.getPath());
            channelServiceAreaVO.mapDataType = mapDataType;

            if (mapDataType == 1) {
                let data = JSON.stringify(BaiduMap.polygon.getPath());
                channelServiceAreaVO.mapData = data;
            } else if (mapDataType == 2) {
                let circle2 = BaiduMap.circle;
                channelServiceAreaVO.mapData = circle2.getRadius();
                channelServiceAreaVO.longitude = circle2.getCenter().lng;
                channelServiceAreaVO.latitude = circle2.getCenter().lat;
            } else {
                channelServiceAreaVO.mapData = null;
            }

            $.ajax({
                async: true,
                type: "POST", url: '/channel/service/area/drawChannelAreaMap.action',
                contentType: "application/json", dataType: "json", data: JSON.stringify(channelServiceAreaVO),
                beforeSend: function () {
                    layLoding();
                },
                success: function (json) {
                    layer.close(index);
                    if (json.status == 200) {
                        zmnLayUtil.closeThisIframeMsg(parent, json.message);
                        parent.reload(json.message);
                    }
                    layer.msg(json.message);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                }
            });
        },
        reset: function () {
            this.initMap(true);
            this.initPolygon();
            this.createBaiduMap();
        },
        resetAll: function () {
            this.initMap(true);
            this.createBaiduMap();
        },
        initMap: function (flag) {
            let lng = parseFloat(channelServiceAreaVO.longitude);
            let lat = parseFloat(channelServiceAreaVO.latitude);

            if (flag) {
                lng = parseFloat(channelServiceAreaVO.baseAreaLongitude);
                lat = parseFloat(channelServiceAreaVO.baseAreaLatitude);
            }

            this.initLng = lng;
            this.initLat = lat;

            var point = new BMap.Point(lng, lat);
            this.point = point;

        },
        /**
         * 根据网点初始化一个小范围
         * 需要父页面提供参数对象：{cityName, longitude, latitude}
         */
        initPolygon: function (mapArgs2) {

            if (mapArgs2) {
                var maps = JSON.parse(mapArgs2);
                var bmap3 = [];
                for (var i = 0; i < maps.length; i++) {
                    var map = maps[i];
                    bmap3.push(new BMap.Point(map.lng, map.lat));
                }

                var polygon3 = new BMap.Polygon(bmap3, {strokeColor: "blue", strokeWeight: 2, strokeOpacity: 0.5});
                this.polygon = polygon3;
            } else {
                let polygonLength = 0.005;
                if(zmnUtil.isEmptyNull(channelServiceAreaVO.countryId)){
                    polygonLength = 0.05;
                }

                // 创建多边形
                var polygon2 = new BMap.Polygon([
                    new BMap.Point(this.initLng + polygonLength, this.initLat + polygonLength),
                    new BMap.Point(this.initLng + polygonLength, this.initLat - polygonLength),
                    new BMap.Point(this.initLng - polygonLength, this.initLat - polygonLength),
                    new BMap.Point(this.initLng - polygonLength, this.initLat + polygonLength)
                ], {
                    strokeColor: "#000",
                    strokeWeight: 2,
                    strokeOpacity: 0.5,
                    enableClicking: false,
                    enableMassClear: false
                });
                this.polygon = polygon2;
            }
        },
        createCicleBaiduMap: function () {
            // 百度地图API功能1
            let map = new BMap.Map("allmap", {enableMapClick: false});
            map.centerAndZoom(this.point, 15);
            map.enableScrollWheelZoom();

            this.initCounty(map);

            //增加圆
            map.addOverlay(BaiduMap.circle);
            this.map = map;
        },
        resetCircle: function () {
            this.initCircle();
            this.createCicleBaiduMap();
        },
        /**
         * 根据网点初始化一个小范围
         * 需要父页面提供参数对象：{longitude, latitude}
         */
        initCircle: function (size) {
            let lng = parseFloat(channelServiceAreaVO.longitude);
            let lat = parseFloat(channelServiceAreaVO.latitude);

            if (!size || isNaN(parseInt(channelServiceAreaVO.mapData))) {
                size = 500;
                if(zmnUtil.isEmptyNull(channelServiceAreaVO.countryId)){
                    size = 5000;
                }
                lng = parseFloat(channelServiceAreaVO.baseAreaLongitude);
                lat = parseFloat(channelServiceAreaVO.baseAreaLatitude);
            }


            let point = new BMap.Point(lng, lat);
            this.point = point;

            // 创建圆
            let circle = new BMap.Circle(point, size, {strokeColor: "blue", strokeWeight: 2, strokeOpacity: 0.9});

            this.point = point;
            this.circle = circle;
        },
        initCounty: function (map) {
            // 初始化行政区域
            let bdary = new BMap.Boundary();
            let name = channelServiceAreaVO.areaName;
            bdary.get(name, function (rs) {       //获取行政区域
                let count = rs.boundaries.length; //行政区域的点有多少个
                for (let i = 0; i < count; i++) {
                    let ply = new BMap.Polyline(rs.boundaries[i], {
                        strokeWeight: 2,
                        strokeColor: "#ff0000",
                        strokeOpacity: 0.5
                    }); //建立多边形覆盖物
                    map.addOverlay(ply);  //添加覆盖物
                    map.setViewport(ply.getPath());    //调整视野
                }
            });
        }
    };

    if (channelServiceAreaVO.mapDataType == 2) {
        BaiduMap.initCircle(channelServiceAreaVO.mapData);
        BaiduMap.createCicleBaiduMap();
    } else if (channelServiceAreaVO.mapDataType == 1) {
        BaiduMap.initMap();
        BaiduMap.initPolygon(channelServiceAreaVO.mapData);
        BaiduMap.createBaiduMap();
    } else {
        BaiduMap.initMap();
        BaiduMap.createBaiduMap();
    }

    $(document).on('click', '#enableEditing ', function () {
        if (mapDataType == 1) {
            BaiduMap.polygon.enableEditing();
        } else {
            BaiduMap.circle.enableEditing();
        }
    });

    $(document).on('click', '#disableEditing ', function () {
        if (mapDataType == 1) {
            BaiduMap.polygon.disableEditing();
        } else {
            BaiduMap.circle.disableEditing();
        }
    });

    $(document).on('click', '#resetMap ', function () {
        if (mapDataType == 1) {
            BaiduMap.reset();
        } else if (mapDataType == 2) {
            BaiduMap.resetCircle();
        } else {
            BaiduMap.resetAll();
        }
    });

    $(document).on('click', '#submitData ', function () {
        BaiduMap.submitData();
    });

    form.on('radio(scope)', function (data) {
        mapDataType = data.value;
        if (data.value == 1) {
            BaiduMap.reset();
        } else if (data.value == 2) {
            BaiduMap.resetCircle();
        } else {
            BaiduMap.resetAll();
        }
    });

    function layLoding() {
        index = layer.load(1, {
            shade: [0.5, '#DBDBDB'] //0.1透明度的白色背景
        });
    }

    exports('channel_area_map_edit', {});
});




